package com.neusoft.elm_servlet.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.neusoft.elm_servlet.dao.DeliveryAddressDao;
import com.neusoft.elm_servlet.po.DeliveryAddress;
import com.neusoft.elm_servlet.util.DBUtil;

public class DeliveryAddressDaoImpl implements DeliveryAddressDao {

	@Override
	public List<DeliveryAddress> listDeliveryAddressByUserId(String userId) throws SQLException {
		Connection con = DBUtil.getConnection();
		PreparedStatement ps = con.prepareStatement("select * from deliveryaddress where userId=?");
		ps.setString(1, userId);
		ResultSet rs = ps.executeQuery();
		List<DeliveryAddress> list = new ArrayList<>();
		while(rs.next()){
			DeliveryAddress da = new DeliveryAddress();
			da.setAddress(rs.getString("address"));
			da.setContactName(rs.getString("contactName"));
			da.setContactSex(rs.getInt("contactSex"));
			da.setContactTel(rs.getString("contactTel"));
			da.setDaId(Integer.valueOf(rs.getString("daId")));
			list.add(da);
		}
		DBUtil.close(rs, ps, con);
		return list;
	}

	@Override
	public int saveDeliveryAddress(DeliveryAddress da) throws SQLException {
		Connection con = DBUtil.getConnection();
		PreparedStatement ps = con.prepareStatement("insert into "
				+ " deliveryaddress values(null,?,?,?,?,?)");
		ps.setString(1, da.getContactName());
		ps.setInt(2, da.getContactSex());
		ps.setString(3, da.getContactTel());
		ps.setString(4, da.getAddress());
		ps.setString(5, da.getUserId());
		int count = ps.executeUpdate();
		DBUtil.close(null, ps, con);
		return count;
	}

	@Override
	public DeliveryAddress getDeliveryAddressById(String daId) throws SQLException {
		Connection con = DBUtil.getConnection();
		PreparedStatement ps = con.prepareStatement("SELECT * FROM deliveryaddress WHERE daId = ? ");
		ps.setString(1, daId);
		ResultSet rs = ps.executeQuery();
		DeliveryAddress da = null;
		while(rs.next()){
			da = new DeliveryAddress();
			da.setAddress(rs.getString("address"));
			da.setContactName(rs.getString("contactName"));
			da.setContactSex(rs.getInt("contactSex"));
			da.setContactTel(rs.getString("contactTel"));
			da.setDaId(Integer.valueOf(rs.getString("daId")));
			da.setUserId(rs.getString("userId"));
		}
		DBUtil.close(rs, ps, con);
		return da;
	}

	
	@Override
	public int updateDeliveryAddress(DeliveryAddress da) throws SQLException {
		Connection con = DBUtil.getConnection();
		PreparedStatement ps = con.prepareStatement("update deliveryaddress set "
				+ " contactName=?,contactSex=?,contactTel=?,address=?,userId=? where daId=?");
		ps.setString(1, da.getContactName());
		ps.setInt(2, da.getContactSex());
		ps.setString(3, da.getContactTel());
		ps.setString(4, da.getAddress());
		ps.setString(5, da.getUserId());
		ps.setInt(6, da.getDaId());
		int count = ps.executeUpdate();
		DBUtil.close(null, ps, con);
		
		return count;
	}

	@Override
	public int removeDeliveryAddress(String daId) throws SQLException {
		Connection con = DBUtil.getConnection();
		PreparedStatement ps = con.prepareStatement("delete from deliveryaddress where daId=?");
		ps.setString(1, daId);
		int count = ps.executeUpdate();
		DBUtil.close(null, ps, con);
		
		return count;
	}

	
}
